layui.use([ 'form', 'layer','upload' ], function() {
	var form  = layui.form, 
		layer = layui.layer,
		upload = layui.upload,
	    $= layui.$ ;
	var id = $('#id').val();
	var roleIds = $('#currUserRoleId').val();
	//拖拽上传
	upload.render({
		elem: '#inputImage'
	    ,url: layui.cache['contentPath']+'/core-common/upload'
	    ,done: function(res){
	    	if(res.code==0){
	    		var img = res.data ;
	    		$('#inputImage').find('img').attr('src', layui.cache['contentPath']+'/'+img.webPath);
	    		$('#imgPath').val(img.webPath);
	    	}
	    }
	});
	
	//查询当前登录用户已有的角色列表,加载到角色的下拉列表中
	$.post(layui.cache['contentPath']+"/sys-role/findRoleListByUser", {}, function(data) {
		data = eval('('+data+')');
		if(data.code=="0"){
			xmSelect.render({
			    el: '#roleIdList',
			    initValue:roleIds.split(","),
			    prop:{name:'name',value:'id'},
			    theme:{color:'#0081ff'},
			    data: data.data,
			    paging: true,
			    pageSize:5,
			    size:'medium',
			    name:"roleIdList"
			});
		}
	});
	//组织机构列表
	$.post(layui.cache['contentPath']+"/sys-org/findList", {}, function(data) {
		data = eval('('+data+')');
		if(data.code=="0"){
			xmSelect.render({
			    el: '#xm-select-orgTrees',
			    initValue:[$('#orgId').val()],
			    prop:{name:'name',value:'id'},
			    theme:{color:'#0081ff'},
			    data: data.data,
			    filterable: true,
			    template({ item, sels, name, value }){
					return item.name  + '<span style="position: absolute; right: 10px; color: #8799a3">'+item.parentName+'</span>' 
				},
			    clickClose:true,
			    paging: true,
			    pageSize:5,
			    radio:true,
			    size:'medium',
			    name:"orgId"
			});
		}
	});
	
	//地区下拉列表 20200222 
	$.post(layui.cache['contentPath']+"/sys-area/findList", {}, function(data) {
		data = eval('('+data+')');
		if(data.code=="0"){
			xmSelect.render({
			    el: '#xm-select-areaTrees',
			    initValue:[$('#areaId').val()],
			    prop:{name:'name',value:'id'},
			    theme:{color:'#0081ff'},
			    data: data.data,
			    filterable: true,
			    template({ item, sels, name, value }){
					return item.name  + '<span style="position: absolute; right: 10px; color: #8799a3">'+item.parentName+'</span>' 
				},
			    clickClose:true,
			    paging: true,
			    pageSize:5,
			    radio:true,
			    name:"areaId"
			});
		}
	});
	
	// 监听提交
	form.on('submit(save)', function(data) {
		var submitUrl = layui.cache['contentPath']+'/sys-user/save';
		if(id!=undefined && id!=""){
			submitUrl = layui.cache['contentPath']+'/sys-user/modify';
		}
		$.ajax({
			type:'POST',
			url:submitUrl,
			data:JSON.stringify(data.field),
			contentType: "application/json",
			success:function(rs){
				rs = eval('('+rs+')');
				if(rs.code==0){
					layer.msg("操作成功",{icon:1,time:3000});
					// 关闭当前frame
					xadmin.close();
					// 可以对父窗口进行刷新
					xadmin.father_reload();
				}else{
					layer.msg(rs.msg,{icon:2,time:3000});
				}
			},
			error:function(status,errors){
				layer.alert('请求错误，错误码：'+JSON.stringify(status));
			}
		});
		return false;
	});
	/**
	 * 自定义校验规则
	 */
	form.verify({
		//登录帐号合法性校验
		confirmLoginAcct: function(value, item){
			if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
				return '用户名不能有特殊字符';
		  	}
		  	if(/(^\_)|(\__)|(\_+$)/.test(value)){
				return '用户名首尾不能出现下划线\'_\'';
		  	}
		}
	});
});